package com.company.mp;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class musicservice extends Service {
    private int progress = 0;
    private boolean isStop = false;

    public musicservice() {
    }

    public class myBinder extends Binder {
        public int getProgress() {
            return progress;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        int act = intent.getIntExtra("action", -1);
        Log.i("asd", "dasfsd...." + act);
        switch (act) {
            case 1:
                play();
                break;
            case 2:
                pause();
                break;

            case 3:
                resume();
                break;
        }

        return super.onStartCommand(intent, flags, startId);
    }

    public void play() {
        thread.start();
    }

    public void pause() {
        isStop = true;
    }

    public synchronized void resume() {
        isStop = false;
        notify();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return new myBinder();
    }


    Thread thread = new Thread() {
        @Override
        public void run() {

            synchronized (musicservice.this) {
                for (int i = 0; i < 300; i++) {
                    if (isStop) {
                        try {
                            musicservice.this.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progress++;
                    Log.i("asfda", progress + "");

                }
            }
        }

    };
}
